package com.mangowang.module.main.other;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import androidx.annotation.Nullable;


import com.mangowang.lib.common.proto.AgentPolicy;

import java.util.ArrayList;
import java.util.List;


/**
 * 文件名 : CricularProgressView
 * 创建者 : MangoWang
 * 创建日期 : 2019/7/5 14:08
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class LevelChangView extends BaseView {

    private Paint DecoratePaint;  // 装饰圆画笔
    private Paint FirstProgressPaint; // 第一进度条画笔
    private Paint SecondProgressPaint; // 第二进度条画笔
    private Paint TextPaint; // 圆的画笔

    private int ProgressFirstColor, ProgressSecondColor; //进度条颜色
    private int DecorateCircularColor; //装饰圆颜色
    private int progress = 0;
    private int anInt;
    private int totalLevel = 0;
    private int thisLevel = 0;
    private int type = 0;
    private List<AgentPolicy.LevelLength> levelS = new ArrayList<>();


    public LevelChangView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    private void initData(Context context, AttributeSet attrs) {


        // 默认颜色
        ProgressFirstColor = Color.parseColor("#CE9A6E");
        ProgressSecondColor = Color.parseColor("#AA6141");
        DecorateCircularColor = Color.parseColor("#D59F72");


    }

    @Override
    protected void init(Context context, AttributeSet attrs) {
        initData(context, attrs);

//        mWidth = dpToPx(100);
        mHeight = dpToPx(100);


        // 初始化画笔
        DecoratePaint = new Paint();
        DecoratePaint.setAntiAlias(true);
        DecoratePaint.setStyle(Paint.Style.FILL);
        DecoratePaint.setColor(DecorateCircularColor);


        // 第一进度条画笔
        FirstProgressPaint = new Paint();
        FirstProgressPaint.setAntiAlias(true);
        FirstProgressPaint.setStyle(Paint.Style.STROKE);
        FirstProgressPaint.setColor(ProgressFirstColor);
        FirstProgressPaint.setStrokeWidth(15);

        // 第二进度条画笔
        SecondProgressPaint = new Paint();
        SecondProgressPaint.setAntiAlias(true);
        SecondProgressPaint.setStyle(Paint.Style.STROKE);
        SecondProgressPaint.setColor(ProgressSecondColor);
        SecondProgressPaint.setStrokeWidth(15);


        TextPaint = new Paint();
        TextPaint.setAntiAlias(true);
        TextPaint.setStyle(Paint.Style.FILL);
        TextPaint.setColor(Color.parseColor("#DDE0E6"));
        TextPaint.setStrokeWidth(0);
        TextPaint.setTextSize(spToPx(10));
        TextPaint.setTextAlign(Paint.Align.CENTER);

    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (levelS.size() == 0) return;
        if (type == 0) {
            canvas.translate(getPMWith() / 2, mCenterY);
            anInt = getPMWith() / 3;
            canvas.drawLine(0, -mCenterY / 2, anInt * (totalLevel - 1), -mCenterY / 2, FirstProgressPaint);
            canvas.drawLine(0, -mCenterY / 2, anInt * (thisLevel + progress / 100f), -mCenterY / 2, SecondProgressPaint);
            for (int i = 0; i <= thisLevel; i++) {
                DecoratePaint.setColor(Color.parseColor("#D59C72"));
                canvas.drawCircle((float) anInt * i, -mCenterY / 2, 20, DecoratePaint);
                DecoratePaint.setColor(Color.parseColor("#FFBB88"));
                canvas.drawCircle((float) anInt * i, -mCenterY / 2, 10, DecoratePaint);

                String progressText = "L" + levelS.get(i).getLevel();
                canvas.drawText(progressText, (float) anInt * i, mCenterY / 6, TextPaint);
                String money = formatInt(Integer.valueOf(levelS.get(i).getAmount()));
                canvas.drawText(money, (float) anInt * i, mCenterY / 2, TextPaint);
            }


            for (int i = thisLevel + 1; i < totalLevel; i++) {
                DecoratePaint.setColor(Color.parseColor("#EEB482"));
                canvas.drawCircle((float) anInt * i, -mCenterY / 2, 20, DecoratePaint);
                DecoratePaint.setColor(Color.parseColor("#FFF9FF"));
                canvas.drawCircle((float) anInt * i, -mCenterY / 2, 10, DecoratePaint);

                String progressText = "L" + levelS.get(i).getLevel();
                canvas.drawText(progressText, (float) anInt * i, mCenterY / 6, TextPaint);
                String money = formatInt(Integer.valueOf(levelS.get(i).getAmount()));
                canvas.drawText(money, (float) anInt * i, mCenterY / 2, TextPaint);
            }

            DecoratePaint.setColor(Color.parseColor("#AA6141"));
            canvas.drawCircle(anInt * (thisLevel + progress / 100f), -mCenterY / 2, 20, DecoratePaint);
            DecoratePaint.setColor(Color.parseColor("#FFF9FF"));
            canvas.drawCircle(anInt * (thisLevel + progress / 100f), -mCenterY / 2, 10, DecoratePaint);
        } else {
            FirstProgressPaint.setColor(Color.parseColor("#FFFFFF"));
            SecondProgressPaint.setColor(Color.parseColor("#EDCCA2"));
            canvas.translate(getPMWith() / 2, mCenterY);
            anInt = getPMWith() / 3;
            canvas.drawLine(0, -mCenterY / 2, anInt * (totalLevel - 1), -mCenterY / 2, FirstProgressPaint);
            canvas.drawLine(0, -mCenterY / 2, anInt * (thisLevel - 1 + progress / 100f), -mCenterY / 2, SecondProgressPaint);

            for (int i = 0; i < thisLevel; i++) {
                DecoratePaint.setColor(Color.parseColor("#FBE9D3"));
                canvas.drawCircle((float) anInt * i, -mCenterY / 2, 20, DecoratePaint);
                DecoratePaint.setColor(Color.parseColor("#FFF9FF"));
                canvas.drawCircle((float) anInt * i, -mCenterY / 2, 10, DecoratePaint);

                String progressText = "L" + levelS.get(i).getLevel();
                canvas.drawText(progressText, (float) anInt * i, mCenterY / 6, TextPaint);
                String money = formatInt(Integer.valueOf(levelS.get(i).getAmount()));
                canvas.drawText(money, (float) anInt * i, mCenterY / 2, TextPaint);
            }


            for (int i = thisLevel; i < totalLevel; i++) {
                DecoratePaint.setColor(Color.parseColor("#EDCCA2"));
                canvas.drawCircle((float) anInt * i, -mCenterY / 2, 20, DecoratePaint);
                DecoratePaint.setColor(Color.parseColor("#FFF9FF"));
                canvas.drawCircle((float) anInt * i, -mCenterY / 2, 10, DecoratePaint);

                String progressText = "L" + levelS.get(i).getLevel();
                canvas.drawText(progressText, (float) anInt * i, mCenterY / 6, TextPaint);
                String money = formatInt(Integer.valueOf(levelS.get(i).getAmount()));
                canvas.drawText(money, (float) anInt * i, mCenterY / 2, TextPaint);
            }

            DecoratePaint.setColor(Color.parseColor("#EDCCA2"));
            canvas.drawCircle(anInt * (thisLevel - 1 + progress / 100f), -mCenterY / 2, 20, DecoratePaint);
            DecoratePaint.setColor(Color.parseColor("#FFF9FF"));
            canvas.drawCircle(anInt * (thisLevel - 1 + progress / 100f), -mCenterY / 2, 10, DecoratePaint);
        }
    }

    private int getPMWith() {
        WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    public void setData(int type, int thisLevel, List<AgentPolicy.LevelLength> levelS, String money, String floats) {
        progress = (int) (Float.parseFloat(floats) * 100);
        this.type = type;
        this.thisLevel = thisLevel;
        this.totalLevel = levelS.size();
        this.levelS = levelS;
        if (thisLevel == totalLevel) progress = 0;
        invalidate();
    }


    private String formatInt(int value) {
        if (value >= 10000) {
            if (value >= 100000000) {
                int n = value / 100000000;
                return n + "亿";
            } else {
                int n = value / 10000;
                return n + "万";
            }
        } else {
            return value + "";
        }
    }

}
